unit fMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, StdCtrls, Menus, Spin;

type
  TformMain = class(TForm)
    pageMain: TPageControl;
    tabTrackOverview: TTabSheet;
    listTrackOverview: TListView;
    RaceTimer: TTimer;
    MainMenu1: TMainMenu;
    Race1: TMenuItem;
    NewRace1: TMenuItem;
    N1: TMenuItem;
    Exit1: TMenuItem;
    tabSeason: TTabSheet;
    panMain: TPanel;
    comTrackSelect: TComboBox;
    btNudge: TButton;
    comDriverSelect: TComboBox;
    btRaceStarter: TButton;
    listTeamPoints: TListView;
    listDriverPoints: TListView;
    ools1: TMenuItem;
    DebugScreen1: TMenuItem;
    About1: TMenuItem;
    tabDebug: TTabSheet;
    reDebug: TRichEdit;
    comYear: TComboBox;
    Panel1: TPanel;
    btRefresh: TButton;
    tabRaceSummary: TTabSheet;
    memoSummary: TMemo;
    btNewSeason: TButton;
    checkDebug: TCheckBox;
    checkWarnings: TCheckBox;
    checkCritical: TCheckBox;
    Pit: TTabSheet;
    trackPitStop1: TTrackBar;
    comTotalStops: TComboBox;
    spinPitLap1: TSpinEdit;
    spinFuelLoad1: TSpinEdit;
    progRaceProgress: TProgressBar;
    trackPitStop2: TTrackBar;
    spinPitLap2: TSpinEdit;
    spinFuelLoad2: TSpinEdit;
    trackPitStop3: TTrackBar;
    spinPitLap3: TSpinEdit;
    spinFuelLoad3: TSpinEdit;
    trackPitStop4: TTrackBar;
    spinPitLap4: TSpinEdit;
    spinFuelLoad4: TSpinEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    comTeamSelect: TComboBox;
    lblTrackName: TLabel;
    btSkip: TButton;
    lblFastestLap: TLabel;
    lblFatestLapValue: TLabel;
    lblOnLapValue: TLabel;
    lblOnLap: TLabel;
    lblSessionNameValue: TLabel;
    lblSessionName: TLabel;
    lblWeather: TLabel;
    btFuel: TButton;
    editFuel: TEdit;
    checkShowFuelLoad: TCheckBox;
    btTyres: TButton;
    Button1: TButton;
    comTyreType: TComboBox;
    btSetWet: TButton;
    btSetDry: TButton;
    btPlusTen: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    btSetBot: TButton;
    btNewRace: TButton;
    tabGraph: TTabSheet;
    Image1: TImage;
    procedure btRaceStarterClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DebugScreen1Click(Sender: TObject);
    procedure btRefreshClick(Sender: TObject);
    procedure checkDebugClick(Sender: TObject);
    procedure checkWarningsClick(Sender: TObject);
    procedure checkCriticalClick(Sender: TObject);
    procedure comTotalStopsChange(Sender: TObject);
    procedure chkAutoPitDriver1Click(Sender: TObject);
    procedure chkAutoPitDriver2Click(Sender: TObject);
    procedure btNewSeasonClick(Sender: TObject);
    procedure btNudgeClick(Sender: TObject);
    procedure btFuelClick(Sender: TObject);
    procedure btTyresClick(Sender: TObject);
    procedure RaceTimerTimer(Sender: TObject);
    procedure btSetWetClick(Sender: TObject);
    procedure btSetDryClick(Sender: TObject);
    procedure btPlusTenClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure btSetBotClick(Sender: TObject);
    procedure btNewRaceClick(Sender: TObject);
    procedure btSkipClick(Sender: TObject);
    procedure btShowTyreTOZClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure RefreshDebug;
    procedure DisplayRace();
    function CheckGame():boolean;
  end;

var
  formMain: TformMain;

implementation

{$R *.dfm}

uses uDebug.common, uSplitter.common, uFuncs.common,

     uDefs.local, uGame.local, uDisplay.local;


procedure TFormMain.DisplayRace();
begin
  lblOnLapValue.Caption := IntToStr( Game.Race.Lap )+' / '+IntToStr( Game.Race.TotalLaps )+' ('+IntToStr(Game.Race.TotalLaps-Game.Race.Lap)+' Remaining) ['+FormatFloat('0.00',(Game.Race.Lap/Game.Race.TotalLaps)*100)+']';
  lblFatestLapValue.Caption := Game.ShowFastestLap();
  with Game.Track(Game.Race.TrackID) do
    lblTrackName.Caption := 'Event: '+Country+'/'+Area+' ['+FormatFloat('0.00', Distance)+'km]';// Grip: '+FormatFloat('0.00',Grip);
  lblSessionNameValue.Caption := Game.Race.Session;
  lblWeather.Caption := ' '+IntToStr(Game.Race.Weather)+'/??'+{IntToStr(Game.Race.Predicted)+}' Temp:'+IntToStr(Game.Race.Temp)+' ';

  lblWeather.Transparent := FALSE;

  if (Game.Race.Weather > 80) then
    lblWeather.Color       := clRed
  else
  if (Game.Race.Weather > 45) then
    lblWeather.Color       := clYellow
  else
    lblWeather.Color       := clLime;

//  DevConsole.Msg(ES_DEBUG, '[DISPLAY]');
  DisplayRaceOverview( Game, ListTrackOverview, comDriverSelect.ItemIndex, checkShowFuelLoad.Checked );
  DisplayDriverPodium(Game, ListDriverPoints, comDriverSelect.Text);
  DisplayTeamPodium(Game, ListTeamPoints, comDriverSelect.Text);

  comTrackSelect.ItemIndex := Game.Race.TrackID;

  DisplayTyreTOZ(Game, Image1);
end;

procedure TformMain.RaceTimerTimer(Sender: TObject);
begin
  btNudgeClick(Sender);
end;

procedure TFormMain.RefreshDebug;
begin
  DevConsole.ExportMessages( reDebug );
end;

function TFormMain.CheckGame():boolean;
begin
  result := TRUE;

  if (Game = nil) then result := FALSE;
end;

procedure TformMain.FormCreate(Sender: TObject);
begin
  randomize;

  DevConsole := TDebugConsole.Create;
//  DevConsole.ShowDebugMessages( TRUE );
  DevConsole.Msg(ES_DEBUG, '[Application Loaded]');

//  Application.Title := Application.
end;

procedure TformMain.btNewRaceClick(Sender: TObject);
begin
//  if (Game <> nil) then
//    FreeAndNil( Game );

//  Game.New(DATA_FILE, comTrackSelect.ItemIndex);

  Game.Race.TrackID := comTrackSelect.ItemIndex;

  DisplayRace();

end;

procedure TformMain.btNewSeasonClick(Sender: TObject);
var D : integer;
begin
  if (Game <> nil) then
    FreeAndNil( Game );

  Game := TGame.Create();
  Game.New( DATA_FILE, -1 );

  SetupRaceOverview( ListTrackOverview, Game.DriverList.Count );
  SetupDriverPodium( ListDriverPoints );
  SetupTeamPodium( ListTeamPoints );

  RefreshDebug; // Export Message to richedit

  comTeamSelect.Items.Clear;
  for D := 0 to High( Game.TeamList.Items ) do
    comTeamSelect.Items.Add(Game.Team(D).Name);
  comTeamSelect.ItemIndex := 2;

  comDriverSelect.Items.Clear;
  for D := 0 to High( Game.DriverList.Items ) do
    comDriverSelect.Items.Add(Game.Driver(D).Name);

  comDriverSelect.ItemIndex := 5;
  Game.DriverList.Items[5].IsBot := FALSE;
//  Game.DriverList.Items[4].IsBot := FALSE;

  comTrackSelect.Items.Clear;
  for D := 0 to High( Game.TrackList.Items ) do
    comTrackSelect.Items.Add( Game.Track(D).Area+' / '+
                              Game.Track(D).Country+' ('+
                              IntToStr(D+1)+'/'+
                              IntToStr( High(Game.TrackList.Items)+1 )+')');

  comTrackSelect.ItemIndex := 0;

//  DevConsole.Msg(ES_DEBUG, '[DISPLAY]');

  DisplayRace();

  comTyreType.Items.Clear;
  for D := 0 to High( Game.TyreList.Items ) do
    comTyreType.Items.Add( Game.TyreList.Items[D].Name );

  comTyreType.ItemIndex := 0;

end;

procedure TformMain.btNudgeClick(Sender: TObject);
begin
//  DevConsole.Msg(ES_DEBUG, '[TICK]');

  if (Game = nil) then
    btNewSeasonClick(Sender)
  else
  begin
    Game.Tick();

    DisplayRace();

    Game.CheckSession(); //Checks for end of sessions and starts the next
  end;
end;

procedure TformMain.btPlusTenClick(Sender: TObject);
begin
  Inc( Game.Race.Predicted, 10);
  if (Game.Race.Predicted > MAX_WEATHER) then
    Game.Race.Predicted := MAX_WEATHER;
end;

procedure TformMain.btFuelClick(Sender: TObject);
begin
  with Game do
  with DriverList.FindByName( formMain.comDriverSelect.Text ) do
    FuelToFill := StrToInt(formMain.editFuel.Text);
end;

procedure TformMain.btRaceStarterClick(Sender: TObject);
begin
  RaceTimer.Enabled := not RaceTimer.Enabled;
  if (RaceTimer.Enabled) then btRacestarter.Caption := 'STOP'
  else btRacestarter.Caption := 'AUTO';
end;

procedure TformMain.btRefreshClick(Sender: TObject);
begin
  RefreshDebug;
end;

procedure TformMain.btSetBotClick(Sender: TObject);
begin
  with Game do
  with DriverList.FindByName( formMain.comDriverSelect.Text ) do
  begin
    IsBot := not IsBot;

    if (IsBot) then
      btSetBot.Caption := 'IsBot'
    else
      btSetBot.Caption := 'IsHuman';
  end;

end;

procedure TformMain.btSetDryClick(Sender: TObject);
begin
  Game.Race.Weather := 1;
  Game.Race.Predicted := 1;
end;

procedure TformMain.btSetWetClick(Sender: TObject);
begin
  Game.Race.Weather := MAX_WEATHER;
  Game.Race.Predicted := MAX_WEATHER;
end;

procedure TformMain.btShowTyreTOZClick(Sender: TObject);
begin
  DisplayTyreTOZ(Game, Image1);
end;

procedure TformMain.btSkipClick(Sender: TObject);
begin
  Game.Race.Lap := Game.Race.TotalLaps;
end;

procedure TformMain.btTyresClick(Sender: TObject);
begin
  with Game do
  with DriverList.FindByName( formMain.comDriverSelect.Text ) do
    TyresToFit := formMain.comTyreType.ItemIndex;
end;

procedure TformMain.Button2Click(Sender: TObject);
begin
  Dec( Game.Race.Predicted, 10);
  if (Game.Race.Predicted < 0) then
    Game.Race.Predicted := 0;
end;

procedure TformMain.Button3Click(Sender: TObject);
begin
  Inc( Game.Race.Predicted, 40);
  if (Game.Race.Predicted > MAX_WEATHER) then
    Game.Race.Predicted := MAX_WEATHER;
end;

procedure TformMain.Button4Click(Sender: TObject);
begin
  Dec( Game.Race.Predicted, 40);
  if (Game.Race.Predicted < 0) then
    Game.Race.Predicted := 0;
end;

procedure TformMain.checkCriticalClick(Sender: TObject);
begin
  DevConsole.ShowCriticalMessages( checkCritical.Checked );
end;

procedure TformMain.checkDebugClick(Sender: TObject);
begin
  DevConsole.ShowDebugMessages( checkDebug.Checked );
end;

procedure TformMain.checkWarningsClick(Sender: TObject);
begin
  DevConsole.ShowWarningMessages( checkWarnings.Checked );
end;

procedure TformMain.chkAutoPitDriver1Click(Sender: TObject);
begin
//  Game.PitDriver1 := chkAutoPitDriver1.Checked;
end;

procedure TformMain.chkAutoPitDriver2Click(Sender: TObject);
begin
//  Game.PitDriver1 := chkAutoPitDriver2.Checked;
end;

procedure TformMain.comTotalStopsChange(Sender: TObject);
begin
  case comTotalStops.ItemIndex of
    0 :begin
      TrackPitStop1.Enabled := FALSE; SpinPitLap1.Enabled := FALSE; SpinFuelLoad1.Enabled := FALSE;
      TrackPitStop2.Enabled := FALSE; SpinPitLap2.Enabled := FALSE; SpinFuelLoad2.Enabled := FALSE;
      TrackPitStop3.Enabled := FALSE; SpinPitLap3.Enabled := FALSE; SpinFuelLoad3.Enabled := FALSE;
      TrackPitStop4.Enabled := FALSE; SpinPitLap4.Enabled := FALSE; SpinFuelLoad4.Enabled := FALSE;
    end;
    1 :begin
      TrackPitStop1.Enabled := TRUE; SpinPitLap1.Enabled := TRUE; SpinFuelLoad1.Enabled := TRUE;
      TrackPitStop2.Enabled := FALSE; SpinPitLap2.Enabled := FALSE; SpinFuelLoad2.Enabled := FALSE;
      TrackPitStop3.Enabled := FALSE; SpinPitLap3.Enabled := FALSE; SpinFuelLoad3.Enabled := FALSE;
      TrackPitStop4.Enabled := FALSE; SpinPitLap4.Enabled := FALSE; SpinFuelLoad4.Enabled := FALSE;
    end;
    2 :begin
      TrackPitStop1.Enabled := TRUE; SpinPitLap1.Enabled := TRUE; SpinFuelLoad1.Enabled := TRUE;
      TrackPitStop2.Enabled := TRUE; SpinPitLap2.Enabled := TRUE; SpinFuelLoad2.Enabled := TRUE;
      TrackPitStop3.Enabled := FALSE; SpinPitLap3.Enabled := FALSE; SpinFuelLoad3.Enabled := FALSE;
      TrackPitStop4.Enabled := FALSE; SpinPitLap4.Enabled := FALSE; SpinFuelLoad4.Enabled := FALSE;
    end;
    3 :begin
      TrackPitStop1.Enabled := TRUE; SpinPitLap1.Enabled := TRUE; SpinFuelLoad1.Enabled := TRUE;
      TrackPitStop2.Enabled := TRUE; SpinPitLap2.Enabled := TRUE; SpinFuelLoad2.Enabled := TRUE;
      TrackPitStop3.Enabled := TRUE; SpinPitLap3.Enabled := TRUE; SpinFuelLoad3.Enabled := TRUE;
      TrackPitStop4.Enabled := FALSE; SpinPitLap4.Enabled := FALSE; SpinFuelLoad4.Enabled := FALSE;
    end;
    4 :begin
      TrackPitStop1.Enabled := TRUE; SpinPitLap1.Enabled := TRUE; SpinFuelLoad1.Enabled := TRUE;
      TrackPitStop2.Enabled := TRUE; SpinPitLap2.Enabled := TRUE; SpinFuelLoad2.Enabled := TRUE;
      TrackPitStop3.Enabled := TRUE; SpinPitLap3.Enabled := TRUE; SpinFuelLoad3.Enabled := TRUE;
      TrackPitStop4.Enabled := TRUE; SpinPitLap4.Enabled := TRUE; SpinFuelLoad4.Enabled := TRUE;
    end;
  end;
end;

procedure TformMain.DebugScreen1Click(Sender: TObject);
begin
  tabDebug.TabVisible := TRUE;
end;

end.
